package com.hung.view.page;

import com.hung.pojo.Account;
import com.hung.pojo.Matter;
import com.hung.service.serviceImpl.MatterServiceImpl;
import com.hung.util.ListToVector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import java.util.Vector;

/**
 * 收藏界面
 *
 * @author Hung
 */
public class FavoriteComponent extends Box {

    private JTable table;
    private Vector<String> title = new Vector<>();
    private Vector<Vector> data = new Vector<>();
    private DefaultTableModel tableModel;

    public FavoriteComponent(Account account) {
        super(BoxLayout.Y_AXIS);

        //标题
        String[] titles = {"事件id", "发布者", "所属瓜圈", "发布时间", "内容"};
        MatterServiceImpl matterService = new MatterServiceImpl();
        List<Matter> matters = matterService.queryFavoriteById(account.getId());

        Vector<Vector> vectors = ListToVector.fListToVector(matters);
        data.clear();
        for (Vector vector : vectors) {
            data.add(vector);
        }
        for (String t : titles) {
            title.add(t);
        }
        //初始化页面
        tableModel = new DefaultTableModel(data, title);
        //设置table
        table = new JTable(tableModel) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        this.add(new JScrollPane(table));
    }
}
